//
//  TT_UserInfoCollectManager.m
//  BaseProject
//
//  Created by 王洋 on 2020/10/21.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "BusinessInfoCollectViewController.h"

#import "TT_UserInfoCollectManager.h"

@implementation TT_UserInfoCollectManager

//未完成项目信息采集实现         //本地标识0的时候弹出，成功提交后将本地标识置1
//+ (void)collectUserInfoWithType:(UserInfoCollectType)type {
    
+ (void)collectUserInfoWithType:(UserInfoCollectType)type Comption:(void (^)(BOOL isClose, BOOL isFinishCollect, NSString *resultStr))comption {

    
    NSString *isCollectInfo = [Utilities getFromNSUserDefaults:@"BA_isUploadInfo"];
    NSString *needInformation = [Utilities getFromNSUserDefaults:@"BA_CollectInfo"];

    //没有上传、且没有收集信息  弹出来收集
    if (![isCollectInfo isEqualToString:@"1"] && needInformation.length == 0) {
        
        switch (type) {
            case UserInfoCollectType_UnFinishProj:{
                
                TT_NavigationViewController *navController = (TT_NavigationViewController *)[TT_TabbarViewController shareTabbar].selectedViewController;
                BusinessInfoCollectViewController *vc = [[BusinessInfoCollectViewController alloc] init];
                vc.completion = ^(BOOL isClose, BOOL isFinishCollect, NSString *resultStr) {
                    if (isClose) {
                        comption(isClose, isFinishCollect, resultStr);
                    }
                };
                
                [navController presentViewController:vc animated:YES completion:nil];
//                [navController pushViewController:vc animated:YES];

            } break;
                
            default:
                break;
        }
    } else { //采集后直接返回  不带结果str 注意 这里的resultStr不是采集结果   是im业务的信息
            comption(YES, YES, @"");
    }
}


+ (void)saveUserInfoWithType:(UserInfoCollectType)type AndInfo:(NSString *)infoString {

    switch (type) {
        case UserInfoCollectType_UnFinishProj:{
            
            [Utilities setToNSUserDefaults:@"BA_CollectInfo" value:infoString];
            
        } break;
            
        default:
            break;
    }
    
}

+ (void)uploadUserInfoWithType:(UserInfoCollectType)type {

    //如果有信息上传   并且弄个标识
    NSString *needInformation = [Utilities getFromNSUserDefaults:@"BA_CollectInfo"];
    if ([needInformation isKindOfClass:[NSString class]] && needInformation.length > 0) {
        [self request_uploadUserInfo:needInformation];
    }
}

+ (void)request_uploadUserInfo:(NSString *)needInformation {
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:[TTUserModel sharedObject].userId forKey:@"userId"];
    [dic setValue:needInformation forKey:@"needInformation"];
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_UserInfoCollect];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        if ([data isKindOfClass:[NSDictionary class]]) {
            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
             if (code == 10000) {
                 
                 //成功后字段置空 BA_isUploadInfo标识改为1  1的时候不采集信息
                 [Utilities setToNSUserDefaults:@"BA_CollectInfo" value:@""];
                 [Utilities setToNSUserDefaults:@"BA_isUploadInfo" value:@"1"];
            }
        }
    } FailedBlock:^(NSError * _Nonnull error) {}];
}

@end
